package com.ganbo.stock.aspect;

import com.ganbo.stock.exception.CusException;
import com.ganbo.stock.utils.ResponseData;
import com.ganbo.stock.enums.ExceptionCodes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Set;


/**
 * 全局异常处理器
 * Created by gan on 2019/10/2 20:31.
 */

@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    @Autowired
    HttpServletRequest request;

    @ExceptionHandler(value = Exception.class)
    public ResponseData globalExceptionHandler(Exception e) {
        if (e instanceof NoHandlerFoundException) {
            log.info("===========404==exception==========url:{},ip:{}", ((NoHandlerFoundException) e).getRequestURL(), request.getRemoteHost());
            return ResponseData.build(false, ExceptionCodes.NOHANDLER);
        } else if (e instanceof CusException) {
            return ResponseData.error(((CusException) e).getCode(), e.getMessage());
        } else if (e instanceof BindException) {
            BindException ex = (BindException) e;
            List<ObjectError> allErrors = ex.getAllErrors();
            ObjectError objectError = allErrors.get(0);
            log.error("参数校验异常,异常信息:{}", allErrors);
            return ResponseData.error(ExceptionCodes.PARAMETER_ERROR.getCode(), objectError.getDefaultMessage());
        } else if (e instanceof ConstraintViolationException) {
            ConstraintViolationException ex = (ConstraintViolationException) e;
            Set<ConstraintViolation<?>> allErrors = ex.getConstraintViolations();
            log.error("单个参数校验异常,异常信息:{}", allErrors);
            return ResponseData.error(ExceptionCodes.PARAMETER_ERROR.getCode(), ex.getMessage());
        } else if (e instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
            BindingResult result = ex.getBindingResult();
            FieldError fieldError = result.getFieldError();
            log.error("单个参数校验异常,异常信息:{}", fieldError);
            return ResponseData.error(ExceptionCodes.PARAMETER_ERROR.getCode(), fieldError.getDefaultMessage());
        } else {
            log.error("系统异常", e);
            return ResponseData.error(ExceptionCodes.SYSTEM_EXCEPTION.getCode(), "系统异常");
        }
    }

}
